//
//  UserViewController.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "SDImageCache.h"
#import "LoginViewController.h"
#import "ActivitySinglton.h"
#import "ColectionViewController.h"
#import "MBProgressHUD.h"
@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain) UIButton *button;

@end

@implementation UserViewController
-(void)viewWillAppear:(BOOL)animated{
#warning     这个方法模态完了之后是不执行的
    [super viewWillAppear:animated];
    //读取登陆状态
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"login"];
    NSLog(@"%@",string);
    if ([string isEqualToString:@"1"]) {
        [self.button setTitle:@"注销" forState:UIControlStateNormal];
        //定状态!!!
        self.button.selected = YES;
    }
    else{
        [self.button setTitle:@"登陆" forState:UIControlStateNormal];
        self.button.selected = NO;
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    
    
    UITableView *mytableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    mytableView.delegate = self;
    mytableView.dataSource = self;
    [self.view addSubview:mytableView];
    
    //注册cell
    [mytableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [mytableView release];
    
    //右导航
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.button];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)rightBarAction:(UIBarButtonItem *)barbutton{
    if (self.button.selected == NO){
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNaVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNaVC animated:YES completion:^{
        }];
        [loginNaVC release];
        [loginVC release];
    }
    
    else{
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出此用户" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
        }];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            //注销清空两个状态
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"login"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserName"];
            
            [self.button setTitle:@"登陆" forState:UIControlStateNormal];
            self.button.selected = NO;
            
            //样式
            MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hub.labelText = @"已注销";
            [hub hide:YES afterDelay:1];
            NSLog(@"注销成功");
            
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消注销");
        }];
        [alertC addAction:action1];
        [alertC addAction:action2];
    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdantifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdantifier];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
    }
    if (indexPath.row == 2){
        cell.textLabel.text = @"我的缓存";
    }
    return cell;
}
//调整高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSLog(@"我的活动");
        ColectionViewController *colectVC = [[ColectionViewController alloc] init];
        [self.navigationController pushViewController:colectVC animated:YES];
        [colectVC release];
    }
    if (indexPath.row == 1) {
        
        NSLog(@"我的电影");
    }
    if (indexPath.row == 2) {
        NSLog(@"我的缓存");
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
